package piwan.gui;

import piwan.*;

import java.io.File;

import javax.media.opengl.GL;

import org.fenggui.Display;
import org.fenggui.FengGUI;
import org.fenggui.Label;
import org.fenggui.binding.render.text.ITextRenderer;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.util.Alignment;
import org.fenggui.util.Color;
import org.fenggui.util.Point;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;

public class StatusBar {
	private final String statusTexFile = "gfx/statusbar-big.png";
	private int pixelHeight;
	private double topCoord;
	private Texture statusTex;
	private SmartButton endbut,nextbut;
	private Display display = null;
	private GL gl = null;
	private Label feedlbl = null;
	private Label statlbl = null;
	private Listener lst = new Listener() {
		public void nextPressed() {}
		public void endPressed() {}
	};
	
	public interface Listener {
		public void endPressed();
		public void nextPressed();
	}
	
	public StatusBar(MainWindow win) {
		try {
			statusTex = TextureIO.newTexture(new File(statusTexFile), false);
		} catch(Exception e) { assert(false); }
		
		display = win.getDisplay();
		gl = win.getGL();
		endbut = FengGUI.createWidget(SmartButton.class);
		nextbut = FengGUI.createWidget(SmartButton.class);
		feedlbl = FengGUI.createWidget(Label.class);
		statlbl = FengGUI.createWidget(Label.class);
		
		display.addWidget(endbut,nextbut,feedlbl,statlbl);
		endbut.init("  End Turn  ");
		nextbut.init("Next Phase >");
		endbut.setPosition( new Point(display.getWidth()-endbut.getWidth()-40, 5));
		nextbut.setPosition( new Point(endbut.getX()-nextbut.getWidth()-20, 5));
		
		pixelHeight = nextbut.getY()+nextbut.getHeight()+10;
		topCoord = -1.0 + (double)pixelHeight/display.getHeight()*2;
		
		feedlbl.setText(" ");
		feedlbl.setPosition( new Point(5,pixelHeight/2-feedlbl.getHeight()/2) );
		
		statlbl.getAppearance().setAlignment(Alignment.MIDDLE);
		statlbl.setText(" ");
		ITextRenderer trStat = statlbl.getAppearance().getRenderer("status");
		ITextRenderer trFeed = statlbl.getAppearance().getRenderer("feedback");
		statlbl.getAppearance().addRenderer("default", trStat);
		feedlbl.getAppearance().addRenderer("default", trFeed);
		
		feedlbl.getAppearance().getStyle("default").getTextStyleEntry("default").setColor(new Color(0.9f,0.9f,0.9f));
		
		nextbut.addButtonPressedListener(new IButtonPressedListener() {
			public void buttonPressed(ButtonPressedEvent e) {
				lst.nextPressed();
			}
		});
		
		endbut.addButtonPressedListener(new IButtonPressedListener() {
			public void buttonPressed(ButtonPressedEvent e) {
				lst.endPressed();
			}
		});
	}
		
	public void setEndEnabled( boolean b ) { endbut.setEnabled(b); }
	public void setNextEnabled( boolean b ) { nextbut.setEnabled(b); }
	public boolean isNextEnabled() { return nextbut.isEnabled(); }
	public boolean isEndEnabled() { return endbut.isEnabled(); }
	
	public void setListener( Listener l ) { lst = l; }
	
	public void setStatus(String str) {
		statlbl.setText(str);
		statlbl.setPosition(new Point(display.getWidth()/2 - statlbl.getWidth()/2,pixelHeight/2-statlbl.getHeight()/2));	
	}
	
	public void feedback(String str) {
		feedlbl.setText(str);
	}
	
	public void setCombatMode( boolean b ) {
		if( b ) {
			nextbut.setText("Next Round >");
			endbut.setText("Cancel Combat");
		} else {
			nextbut.setText("Next Phase >");
			endbut.setText("  End Turn  ");
		}
	}
	
	public void setCloseWindow(boolean b) {
		if(b)
			endbut.setText("Close Window");
		else
			endbut.setText("Cancel Combat");
	}
	
	public void render() {
		GameGui.prepareGuiRender(gl, true);
		statusTex.bind();
		gl.glBegin(GL.GL_QUADS);
		gl.glColor4d(1.0,1.0,1.0,1.0);
		gl.glTexCoord2d(0.0,1.0);
		gl.glVertex2d(-1.0,-1.0);
		gl.glTexCoord2d(1.0,1.0);
		gl.glVertex2d(1.0,-1.0);
		gl.glTexCoord2d(1.0,0.0);
		gl.glVertex2d(1.0,topCoord);
		gl.glTexCoord2d(0.0,0.0);
		gl.glVertex2d(-1.0,topCoord);
		gl.glEnd();
	}
}